/**
 * 
 */
package dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.sql.DataSource;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author qiyuan.zqy
 *
 */
public class GenericDAO {
	
private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		        this.jdbcTemplate = new JdbcTemplate(dataSource); 
	}
	
	public List<Map> query(String tableName, Map<String,Object> conditions){
		
		StringBuffer sql = new StringBuffer("select * from ");
		sql.append(tableName);
		
		int conSize = conditions.size();
		Object[] args = new Object[conSize];
		int i = 0;
		if ((conditions != null) && (conSize > 0)){
			sql.append(" where ");
		
			for(Entry entry : conditions.entrySet()){
				sql.append((String)entry.getKey());
				sql.append("= ? ");
				args[i] = entry.getValue();
				
				if (i < conSize -1){
					sql.append(" and ");
				}
				i++;
			
			}
		}
		
//		System.out.println(sql.toString());
		
		List<Map> rows = this.jdbcTemplate.queryForList(sql.toString(), args);
		return rows;
		
	}
	
	public void insert(String tableName,final List<String> columns,final List<Map<String,Object>>values){
		StringBuffer header=new StringBuffer("INSERT into ");
		StringBuffer body=new StringBuffer(" VALUES(");
		header.append(tableName+"(");
		for(int i=0;i<columns.size();i++){
			header.append(columns.get(i));
			if(i!=columns.size()-1){
				header.append(",");
				body.append("?,");
			}
			else{
				header.append(")");
				body.append("?)");
			}
		}
		
		String sql=header.append(body.toString()).toString();
		System.out.println(sql);
		this.jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() 
		  	{ 
			   public void setValues(PreparedStatement ps,int i)throws SQLException 
			   { 
				   Map<String ,Object> value=values.get(i);
				   for(int j=0;j<columns.size();j++){
					   ps.setObject(j+1, value.get(columns.get(j)));
				   }
			   } 
			   public int getBatchSize() 
			   { 
			    return values.size(); 
			   } 
			  });
	}
	
	public void delete(String tableName){
		String sqlStr = "delete from " + tableName;
		
		System.out.println(sqlStr);
		
		this.jdbcTemplate.update(sqlStr);

	}
	
	public static void main(String[] args){
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("dao-beans.xml");
		GenericDAO dao=(GenericDAO) context.getBean("deguicDao");
		
		String tableName = "deg_users_main";
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("user_id", 1406081358422482L);
		List<Map> result = dao.query(tableName, conditions);
		
		if ((result == null)||(result.size()==0)){
			System.out.println("result is null or size is 0");
			return;
		}
		
		for (int i=0; i<result.size(); i++){
			System.out.println("Item " + i + " is:");
			Map itemMap = result.get(i);
			
			for (Object key:itemMap.keySet()){
				System.out.print(key+" = ");
				System.out.println(itemMap.get(key));
				
			}
		}
		
		
	}

}
